/*--------------------环境模块.设备管理--ly-----------------*/
/*--------------------设备信息修改功能 en_equipment_edit.html------------------*/


$(document).ready(function() {
	var deviceid = $.cookie("deviceid");
	var editcookie = $.cookie("nav");
	var Wtid = JSON.parse(editcookie).w;

	//填充数据
	var searchurl = environmentport + '/DeviceController/selectByPrimaryKey'; //查询
	$.get(searchurl, {
		'deviceid': deviceid
	}, function(result) {
		console.log(result);
		var code = result.code;
		var editlist = result.object;
		if(code == 0) {
			$("#L_name").val(editlist.name); //设备名称
			$("#L_type").val(editlist.type); //设备类型
			$("#L_qrcode").val(editlist.qrcode); //二维码
			$("#L_property").val(editlist.property); //所有权
			$("#L_manager").val(editlist.manager); //管理员
			$("#L_operator").val(editlist.operator); //操作员
			$("#L_picture").val(editlist.picture); //设备图片
			$("#L_position").val(editlist.position); //设备定位
			$("#L_institutionid").val(editlist.institutionid); //所属机构
			$("#L_produceddate").val(editlist.produceddate); //生产日期
			$("#L_leavefactorydate").val(editlist.leavefactorydate); //出厂日期
		} else {
			console.log("网络不佳，稍后再试！")
		}
	});

	//layui
	layui.use(['form', 'layer'], function() {
		$ = layui.jquery;
		var form = layui.form,
			layer = layui.layer;

		//自定义验证规则
		form.verify({
			name: function(value) {
				if(value.length < 5) {
					return '昵称至少得5个字符啊';
				}
			},
			
		});

		//获取表单数据
		$("input").blur(function() {
			//失去焦点事件
			var name = $("#L_name").val(); //设备名称
			var type = $("#L_type").val(); //设备类型
			var qrcode = $("#L_qrcode").val(); //二维码
			var property =$("#L_property").val(); //所有权
			var manager = $("#L_manager").val(); //管理员
			var operator = $("#L_operator").val(); //操作员
			var picture = $("#L_picture").val(); //设备图片
			var position = $("#L_position").val(); //设备定位
			var institutionid = $("#L_institutionid").val(); //所属机构
			//监听提交
			form.on('submit(edit)', function() {
				var produceddate = $("#L_produceddate").val(); //生产日期
				var leavefactorydate = $("#L_leavefactorydate").val(); //出厂日期
				//递交修改
				editData(name, type, qrcode, property, manager, operator, picture,position,institutionid,produceddate,leavefactorydate);
				return false;
			});
		});

		//修改用户信息
		var editurl = environmentport + '/DeviceController/modifyByDeviceid'; //修改
		// 获得frame索引
		var index = parent.layer.getFrameIndex(window.name);
		var editData = function(name, type, qrcode, property, manager, operator, picture,position,produceddate,leavefactorydate) {
			//修改 edit
			$.get(editurl, {
				deviceid: deviceid,
				name: name,
				type: type,
				qrcode:qrcode,
				property:property,
				manager:manager,
				operator:operator,
				picture:picture,
				position:position,
//				produceddate:produceddate,
				leavefactorydate:leavefactorydate
			}, function(result) {
				var code = result.code;
				if(code == 0) {
					layer.msg('修改成功!', {
						icon: 1,
						time: 1000
					}, function() {
						//关闭当前frame
						parent.layer.close(index);
					});
				} else {
					layer.msg('网络开小差了，请稍后再试!', {
						icon: 2,
						time: 1000
					}, function() {
						//关闭当前frame
//						parent.layer.close(index);
					});
				}
			})
		}

	});
})